package org.example.demo_ottery.dao.mapper;

import org.apache.ibatis.annotations.*;
import org.example.demo_ottery.dao.dataobject.Encrypt;
import org.example.demo_ottery.dao.dataobject.UserDO;

import java.util.List;

@Mapper
public interface UserMapper {
    /**
     * 获取邮箱绑定的用户数
     * @param email
     * @return
     */
    @Select("select count(*) from user where email=#{email}")
    int countByMail(@Param("email") String email);

    /**
     * 获取手机号绑定的用户数
     * @param phoneNumber
     * @return
     */
    @Select("select count(*) from user where phone_number=#{phoneNumber}")
    int countByPhoneNumber(@Param("phoneNumber") Encrypt phoneNumber);

    /**
     * 注册用户
     * @param userDO
     */
    @Insert("insert into user (user_name,email,phone_number,identity,password)"+
    " values (#{userName},#{email},#{phoneNumber},#{identity},#{password})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void insert(UserDO userDO);

    @Select("select * from user where phone_number=#{phoneNumber}")
    UserDO selectByPhoneNumber(@Param("phoneNumber") Encrypt phoneNumber);

    @Select("select * from user where email=#{email}")
    UserDO selectByMail(@Param("email") String email);

    @Select("<script>" +
            " select * from user" +
            " <if test=\"identity!=null\">" +
            "    where identity = #{identity}" +
            " </if>" +
            " order by id desc" +
            " </script>")
    List<UserDO> selectUserListByIdentity(@Param("identity") String identity);

    @Select("<script>" +
            " select id from user" +
            " where id in" +
            " <foreach collection='items' item='item' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<Long> selectExistByIds(@Param("items") List<Long> userIds);

    @Select("<script>" +
            " select * from user" +
            " where id in" +
            " <foreach collection='items' item='item' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<UserDO> batchSelectByIds(@Param("items") List<Long> userIds);
}
